package com.lww.integral.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lww.integral.service.ExchangeService;
import com.lww.springcloudapi.entity.TabGradeLog;
import com.lww.springcloudapi.result.Result;
import com.lww.springcloudapi.result.ResultCodeEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 积分兑换
 * </p>
 *
 * @author SHEN
 * @since 2021-03-11
 */
@Api(description = "积分兑换")
@RestController
@RequestMapping("/integral")
public class ExchangeServiceController {

    @Autowired
    private ExchangeService exchangeService;

    @ApiOperation(value = "根据商品id 电话号码来积分兑换 用户调用接口")
    @PostMapping("/exchange")
//    @PreAuthorize("hasAnyAuthority('admin')")
    public Result findPointsExchange(Integer cid, String phone) {
        try {
            boolean pointsExchange = exchangeService.findPointsExchange(phone, cid);
            if (!pointsExchange) {
                return Result.build(pointsExchange, ResultCodeEnum.PARAM_ERROR);
            }
            return Result.ok(pointsExchange);
        } catch (Exception e) {
            return Result.fail(ResultCodeEnum.FAIL);
        }
    }

    /**
     * 132 查询所有记录
     *
     * @return
     */
    @ApiOperation(value = "查询所有记录 用户调用接口")
    @PostMapping("/findQuery")
//    @PreAuthorize("hasAnyAuthority('admin')")
    public Result findQuery(@RequestParam(required = false, defaultValue = "1") Integer pageNo,
                            @RequestParam(required = false, defaultValue = "2") Integer pageSize) {

        System.out.println(pageNo);
        try {
            Page query = exchangeService.findQuery(pageNo, pageSize);
            return Result.ok(query);
        } catch (Exception e) {
            return Result.fail(ResultCodeEnum.FAIL);
        }
    }

    /**
     * 根据id删除记录
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "根据id删除记录 用户调用接口")
    @PostMapping("/delGradeLog/{id}")
//    @PreAuthorize("hasAnyAuthority('admin')")
    public Result findDelGradeLog(@PathVariable("id") Integer id) {
        try {
            boolean delGradeLog = exchangeService.findDelGradeLog(id);
            if (!delGradeLog) {
                return Result.build(delGradeLog, ResultCodeEnum.PARAM_ERROR);
            }
            return Result.ok(delGradeLog);
        } catch (Exception e) {
            return Result.fail(ResultCodeEnum.FAIL);
        }
    }

}
